<?php if(!defined('ARCH_ROOT_PATH')) exit;
/**
 *	Architect Framework
 *
 *	Architect Framework is a light weight object oriented web applications framework written for PHP 5.1.4 and later that provide the tools to develop and deploy a website or application fast and with as little code as possible.
 *
 *	@author Robin Grass, <hello@coffinboy.se>
 *	@link http://architect.kodlabbet.net
 *	@license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License (v. 3)
 */

/**
 *	Architect_URI
 *
 *	This class is pretty simple, it handles some basic URI manipulation and parsing. It also adds a "missing feature", which is the current URI in your adress field.
 *
 *	@package Runtime
 *	@version 1.0
 *	@author Robin Grass, <hello@coffinboy.se>
 */
class Architect_URI {

	/**
	 *	getLocation
	 *
	 *	This method creates and returns the current URI from your address field. This method has a lot of logic because PHP does not have a native feature for this.
	 *
	 *	@return string
	 */
	public function getLocation() {
		$protocol = 'http';
		if(array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] == 'on')
			$protocol .= 's';
		$protocol .= '://';
		if($_SERVER['SERVER_PORT'] != '80')
			$url = $protocol . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
		else $url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
	return $url;
	}

	/**
	 *	getVirtual
	 *
	 *	This method returns the "virtual" URI, which is the parts after the base URL and/or domain name.
	 *
	 *	@return string
	 */
	public function getVirtual() {
		return trim(str_ireplace($_SERVER['SCRIPT_NAME'], '', $_SERVER['PHP_SELF']), '/');
	}
	
	/**
	 *	getBase
	 *
	 *	This method returns the URI base, which probably is your domain name.
	 */
	public function getBase() {
		$path = str_ireplace($this->getVirtual(), '', $this->getLocation());
		$file = preg_quote(substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], '/')));
	return str_ireplace(stripslashes($file), '', trim($path, '/'));
	}
	
	/**
	 *	getSegment
	 *
	 *	This method returns a segment from the virtual URI.
	 *
	 *	@param int $n
	 *
	 *	@return string
	 */
	public function getSegment($n = 1) {
		$segments = explode('/', $this->getVirtual());
		if($n > 0 && $n <= count($segments))
			return $segments[$n - 1];
	}
	
}
?>